/*
 * Created by JFormDesigner on Tue Apr 17 23:55:07 CEST 2012
 */

package info.rflow.ui;

import info.rflow.SessionManager;
import info.rflow.connectors.ConnectorUtils;
import info.rflow.model.RBlock;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Utilities;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


/**
 * @author Holger Brandl
 */
public class SourceArea extends JPanel {

    private RBlock curBlock;

    private SessionManager sessionManager;


    public SourceArea() {
        initComponents();
        textArea1.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent documentEvent) {
                textChanged();
            }


            @Override
            public void removeUpdate(DocumentEvent documentEvent) {
                textChanged();
            }


            @Override
            public void changedUpdate(DocumentEvent documentEvent) {
                textChanged();
            }
        });

        textArea1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent keyEvent) {
                if (keyEvent.isMetaDown()) {
                    if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
                        // get line or selection
                        String selection = textArea1.getSelectedText();
                        if (selection == null || selection.trim().isEmpty()) {
                            int caretPosition = getCaretPosition();
                            if (caretPosition < 1) {
                                return;
                            }

                            selection = textArea1.getText().split("\\r?\\n")[caretPosition - 1];
                        }

                        ConnectorUtils.push2R(selection);
                    }
                }
            }


            public int getCaretPosition() {
                int caretPos = textArea1.getCaretPosition();
                int rowNum = (caretPos == 0) ? 1 : 0;
                for (int offset = caretPos; offset > 0; ) {
                    try {
                        offset = Utilities.getRowStart(textArea1, offset) - 1;
                    } catch (BadLocationException e) {
                        e.printStackTrace();
                    }

                    rowNum++;
                }

                return rowNum;
            }
        });
    }


    public void setBlock(RBlock rBlock) {
        curBlock = null;
        textArea1.setText(rBlock.getCode());
        curBlock = rBlock;


        textArea1.setCaretPosition(0);
    }


    public void setSessionManager(SessionManager sessionManager) {
        this.sessionManager = sessionManager;
    }


    public void textChanged() {
        if (curBlock != null) {
            boolean hasBlockCodeChanged = !curBlock.getCode().equals(textArea1.getText());
            if (hasBlockCodeChanged) {
                curBlock.setCode(textArea1.getText());
                sessionManager.getActiveWorkflow().setChanged(true);
            }
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner non-commercial license
        panel1 = new JScrollPane();
        textArea1 = new JTextArea();

        //======== this ========
        setMinimumSize(null);
        setMaximumSize(null);
        setPreferredSize(null);
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setViewportView(textArea1);
        }
        add(panel1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }


    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner non-commercial license
    private JScrollPane panel1;
    private JTextArea textArea1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables


}
